
<title>Shipping Services</title>


<?php    

/*

***************************************
*                                     *
* Copyright 2011 David Matthew Pugh   *
* Contact Info:thebravedave@gmail.com *
*                                     *
***************************************

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once "../services/webservices.php";
require_once "index.php";
require_once("../Pagination.php");
  
   
echo '<br /><br />';
echo '<div class="smallcorerightmargin">';   
echo '<br /><b>Current Carriers And Services</b><br /><br />';

$upsservicearray = array();
$upsservicearray["03"] = "ground";
$upsservicearray["01"] = "next day air";
$upsservicearray["02"] = "second day air";
$upsweightmaxarray = array();
$upsweightmaxarray["03"] = 150;
$upsweightmaxarray["01"] = 150;
$upsweightmaxarray["02"] = 150;


$uspsservicearray = array();
$uspsservicearray["parcel"] = "parcel";
$uspsservicearray["express"] = "express";
$uspsservicearray["priority"] = "priority";
$uspsweightarray = array();
$uspsweightmaxarray["parcel"] = 70;
$uspsweightmaxarray["express"] = 70;
$uspsweightmaxarray["priority"] = 70;
$shipping = new shipping(); 

if(isset($_GET['deleteservice']))
{
  
    $id = $_GET['shippingserviceid'];
    $shipping->service_id = $id;
    $shipping->deleteservice();
    
}

if(isset($_GET['addservicetocarrier']))
{
    if(isset($_GET['UPS']))
    {
        
        $carrier_id = $_GET['UPS'];
        $service_id = $_GET['addservicetocarrier'];
        $weight_max = $upsweightmaxarray[$service_id];
        $service = $upsservicearray[$service_id];
        
      
       
        $shipping->service_id = $service_id;
        $shipping->service = $service;
        $shipping->weight_max = $weight_max;
        $shipping->carrier_id = $carrier_id;
        $shipping->addshippingservice();
      
    }
    if(isset($_GET['USPS']))
    {
        $carrier_id = $_GET['USPS'];
        $service_id = $_GET['addservicetocarrier'];   
        $weight_max = $uspsweightmaxarray[$service_id];
        $service = $uspsservicearray[$service_id];
        $shipping->service_id = $service_id;
        $shipping->service = $service;
        $shipping->weight_max = $weight_max;
        $shipping->carrier_id = $carrier_id;

        
        $shipping->addshippingservice();
    }
    
}


$carrierReturn = $shipping->getshippingcarriers();
$carrierXml = new SimpleXMLElement($carrierReturn);
$carrierCount = $carrierXml->Item_Count;
if($carrierCount > 0)
{
    foreach($carrierXml->Item as $carrierItem)
    {
        $carrier_id = $carrierItem->carrier_id;
        $carrier_name = $carrierItem->carrier_name;        
        echo "<b>Carrier</b><br />$carrier_name <br /><br />";
        echo "<b>Services</b><br />";
        echo "<form action='./shippingservices.php' method='GET'>";
        echo "<select name='shippingserviceid'>";
        
        
        $shipping->carrier_id = $carrier_id;
        $servicesReturn = $shipping->getshippingservicebycarrierid();
        $servicesXml = new SimpleXMLElement($servicesReturn);
        $servicesCount = $servicesXml->Item_Count;
        if($servicesCount > 0)
        {
            foreach($servicesXml->Item as $servicesItem)
            {   
                $id = $servicesItem->id;
                $service_id = $servicesItem->service_id;
                $service = $servicesItem->service;
                echo "<option value='$id'>$service</option>";
            }
            echo "</select><br />";
            echo "<input type='submit' name='deleteservice' value='delete service'/>";
            echo "</form>";        
        }
        else
        {
            echo '<option value="none">No Services Have Been Added To This Carrier Yet</option>';
            echo "</select><br />";
            echo "</form>";           
        } 
        
    } 
    echo '</div>';
    
   echo '<div class="smallcorerightmargin">'; 
    echo  '';
    echo '<b>Add Services To Carrier</b><br /><br />';
   
    
    foreach($carrierXml->Item as $carrierItem)
    {
        $array = array();
        $carrier_name = (string)$carrierItem->carrier_name;
        $carrier_id = (int)$carrierItem->carrier_id;
       
        if($carrier_name == "UPS")
        {
           $array = $upsservicearray; 
        }
        else
        {
            $array = $uspsservicearray;  
        }
       
        
        echo '<b>Carrier:</b>' . $carrier_name . '<br />';
        echo '<form action="./shippingservices.php" method="GET">';
        echo '<b>Service To Add:</b><select name="addservicetocarrier">';
      
        
        $shipping->carrier_id = $carrier_id;
        $servicesReturn = $shipping->getshippingservicebycarrierid();
        $servicesXml = new SimpleXMLElement($servicesReturn);
      
        $current_services_array = array();
        foreach($servicesXml->Item as $servicesItem)
        {
            $service = (string)$servicesItem->service; 
            $service_id = (string)$servicesItem->service_id;
            $current_services_array[$service_id] = $service;
           
        }

        $count = 0;
        foreach($array as $currentservice_id=>$currentservice)
        {
           
            if(!in_array($currentservice, $current_services_array))
            {
               echo "<option value='$currentservice_id'>$currentservice</option>";
               $count++; 
            } 
             
        } 
        
        if($count == 0)
        {
            echo '<option value="noservices">all services have been added</option>';
        }
        
        echo '</select>';
        echo "<input type='hidden' name='$carrier_name' value='$carrier_id'/>";
        if($count != 0)
        {
            echo '<br /><input type="submit" name="addservicetocarriersubmit" value="add service" />';
        }
        
        echo '</form>';
        
    }
    echo '</div>';
          
}
else
{
    echo '<b>No Carriers Are Assigned Yet</b>';
    
}



